﻿using WMLogistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper;
using DapperExtensions;
using System.Data;

namespace WMLogistics_Dao
{
    public class CityBaseDao
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int CityBaseAdd(wuliu_city_base model)
        {
            using (IDbConnection conn = ConnectionHelper.OpenConnection())
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into wuliu_city_base(");
                strSql.Append("city_name,state,type,sub_city_id,country)");
                strSql.Append(" values (");
                strSql.Append("@city_name,@state,@type,@sub_city_id,@country)");
                return conn.Execute(strSql.ToString(), model);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int CityBaseUpdate(wuliu_city_base model)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update wuliu_city_base set ");
                if (model.city_name.IfNotNull())
                    strSql.Append("city_name=@city_name,");
                if (model.state.IfNotNull())
                    strSql.Append("state=@state,");
                if (model.type.IfNotNull())
                    strSql.Append("type=@type,");
                if (model.sub_city_id.IfNotNull())
                    strSql.Append("sub_city_id=@sub_city_id,");
                if (model.country.IfNotNull())
                    strSql.Append("country=@country");
                strSql.Append(" where city_id=@city_id ");
                return conn.Execute(strSql.ToString(), model);
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<wuliu_city_base> GetCityBaseList(wuliu_city_base model)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select city_id,city_name,state,type,sub_city_id,country ");
                strSql.Append(" FROM wuliu_city_base  where 1=1 ");
                if (model.city_name.IfNotNull())
                    strSql.Append(" and city_name=@city_name");
                if (model.state.IfNotNull())
                    strSql.Append(" and state=@state");
                if (model.type.IfNotNull())
                    strSql.Append(" and type=@type");
                if (model.sub_city_id.IfNotNull())
                    strSql.Append(" and sub_city_id=@sub_city_id");
                if (model.country.IfNotNull())
                    strSql.Append(" and country=@country");
                return conn.Query<wuliu_city_base>(strSql.ToString(), model).ToList();
            }
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<wuliu_city_base> GetCityBussinList(wuliu_city_base model)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select city_id,city_name,state,type,sub_city_id,country ");
                strSql.Append(" FROM wuliu_city_base  where 1=1 ");
                if (model.city_name.IfNotNull())
                    strSql.Append(" and city_name=@city_name");
                if (model.state.IfNotNull())
                    strSql.Append(" and state=@state");
                if (model.type.IfNotNull())
                    strSql.Append(" and type=@type");
                if (model.sub_city_id.IfNotNull())
                    strSql.Append(" and sub_city_id=@sub_city_id");
                if (model.country.IfNotNull())
                    strSql.Append(" and country=@country");
                return conn.Query<wuliu_city_base>(strSql.ToString(), model).ToList();
            }
        }
        /// <summary>
        /// 获取城市数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public wuliu_city_base GetCityBase(int id)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Get<wuliu_city_base>(id);
            }
        }



    }
}
